home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / stix / Point.st < prev    next >
Text File  |  1991-09-13  |  4KB  |  230 lines

  1. "=====================================================================
  2. |    Point Class Definitions
  3.  ====================================================================="
  4.  
  5.  
  6. "======================================================================
  7. |
  8. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  9. | By Doug McCallum <uunet!ico.isc.com!dougm>
  10. | Additions by sbb@eng.sun.com (Steve Byrne)
  11. |
  12. | This file is part of GNU Smalltalk.
  13. |
  14. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  15. | under the terms of the GNU General Public License as published by the Free
  16. | Software Foundation; either version 1, or (at your option) any later version.
  17. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  18. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  20. | details.
  21. | You should have received a copy of the GNU General Public License along with
  22. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  23. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  24. |
  25.  ======================================================================"
  26.  
  27.  
  28.  
  29.  
  30. "
  31. |     Change Log
  32. | ============================================================================
  33. | Author       Date       Change
  34. | sbb         13 Sep 91      Added = for Points (= from Object isn't right)
  35. |
  36. | dougm      16 Apr 90    Created basic Point class.
  37. |
  38. "
  39.  
  40. Object subclass: #Point
  41.        instanceVariableNames: 'x y'
  42.        classVariableNames: ''
  43.        poolDictionaries: ''
  44.        category: nil !
  45.  
  46. Point comment:
  47. 'Beginning of a Point class for simple display manipulation.  Has not been
  48.  exhaustively tested but appears to work for the basic primitives and for
  49.  the needs of the Rectangle class.' !
  50.  
  51. !Number methodsFor: 'point creation'!
  52.  
  53. @ y
  54.     ^ Point x: self y: y
  55. !
  56.  
  57. asPoint
  58.     ^Point x: self y: self
  59. !!
  60.  
  61.  
  62. !Point class methodsFor: 'instance creation'!
  63.  
  64. x: xInteger y: yInteger
  65.     ^self new x: xInteger y: yInteger
  66. !!
  67.  
  68. !Point methodsFor: 'printing'!
  69.  
  70. printOn: aStream
  71.     x printOn: aStream.
  72.     '@' printOn: aStream.
  73.     y printOn: aStream
  74. !!
  75.  
  76. !Point methodsFor: 'storing'!
  77.  
  78. storeOn: aStream
  79.     x storeOn: aStream.
  80.     '@' storeOn: aStream.
  81.     y storeOn: aStream
  82. !!
  83.  
  84. !Point methodsFor: 'accessing'!
  85.  
  86. x
  87.     ^x
  88. !
  89.  
  90. y
  91.     ^y
  92. !
  93.  
  94. x: aNumber
  95.     x _ aNumber
  96. !
  97.  
  98. y: aNumber
  99.     y _ aNumber
  100. !
  101.  
  102. x: anXNumber y: aYNumber
  103.     x _ anXNumber.
  104.     y _ aYNumber
  105. !!
  106.  
  107. !Point methodsFor: 'converting'!
  108.  
  109. asPoint
  110.     ^self            "But I already AM a point!"
  111. !
  112.  
  113. hash
  114.     ^(x abs + y abs) truncated
  115. !!
  116.  
  117. !Point methodsFor: 'arithmetic'!
  118.  
  119. + delta
  120.     delta _ delta asPoint.
  121.     ^Point x: (x + delta x) y: (y + delta y)
  122. !
  123.  
  124. - delta
  125.     delta _ delta asPoint.
  126.     ^Point x: (x - delta x) y: (y - delta y)
  127. !
  128.  
  129. * scale
  130.     scale _ scale asPoint.
  131.     ^Point x: (x * scale x) y: (y * scale y)
  132. !
  133.  
  134. / scale
  135.     scale _ scale asPoint.
  136.     ^Point x: (x / scale x) y: (y / scale y)
  137. !
  138.  
  139. // scale
  140.     scale _ scale asPoint.
  141.     ^Point x: (x // scale x) y: (y // scale y)
  142. !
  143.  
  144. abs
  145.     ^Point x: (x abs) y: (y abs)
  146. !!
  147.  
  148. !Point methodsFor: 'truncation and round off'!
  149.  
  150. rounded
  151.     ^Point x: (x rounded) y: (y rounded)
  152. !
  153.  
  154. truncateTo: grid
  155.     ^Point x: (x truncateTo: grid) y: (y truncateTo: grid)
  156.  
  157. !!
  158.  
  159.  
  160. !Point methodsFor: 'comparing'!
  161.  
  162. = aPoint
  163.     ^(x = aPoint x) and: [ y = aPoint y ]
  164. !
  165.  
  166. < aPoint
  167.     ^(x < aPoint x) and: [ (y < aPoint y) ]
  168. !
  169.  
  170. > aPoint
  171.     ^(x > aPoint x) and: [ (y > aPoint y) ]
  172. !
  173.  
  174. <= aPoint
  175.     ^(self > aPoint) not    "unverified"
  176. !
  177.  
  178. >= aPoint
  179.     ^(self < aPoint) not    "unverified"
  180. !
  181.  
  182. max: aPoint
  183.     (self >aPoint )
  184.        ifTrue: [ ^self ]
  185.        ifFalse:[ ^aPoint ]
  186. !
  187.  
  188. min: aPoint
  189.     (self < aPoint)
  190.        ifTrue: [^ self ]
  191.        ifFalse:[^ aPoint ]
  192. !!
  193.  
  194. !Point methodsFor: 'point functions'!
  195.  
  196. dist: aPoint
  197.     | a b |
  198.     a _ x - (aPoint x).
  199.     b _ y - (aPoint y).
  200.     ^((a squared) + (b squared)) sqrt
  201. !
  202.  
  203. dotProduct: aPoint
  204.     ^(x * aPoint x) + (y * aPoint y)
  205. !
  206.  
  207. grid: aPoint
  208.     ^Point x: (x roundTo: (aPoint x)) y: (y roundTo: (aPoint y))
  209. !
  210.  
  211. normal
  212. "rotate the Point 90degrees clockwise and get the unit vector"
  213.     |len|
  214.     len _ ((x squared) + (y squared)) sqrt.
  215.     ^Point x: ((y asFloat negated)/len) y: (x/len)
  216. !
  217.  
  218. transpose
  219.     ^Point x: y y: x
  220. !
  221.  
  222. truncatedGrid: aPoint
  223.     ^Point x: (x truncateTo: (aPoint x)) y: (y truncateTo: (aPoint y))
  224. !!
  225.  
  226.  
  227.  
  228.